package com.yami.shop.api.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.Agent;
import com.yami.shop.bean.model.LuckUser;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.util.SecurityUtils;
import com.yami.shop.service.LuckUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/luckUser")
public class LuckUserController {

    @Autowired
    private LuckUserService luckUserService;

    /**
     * 获取信息
     */
//    @GetMapping("/info/{uid}")
//    public ResponseEntity<LuckUser> info(@PathVariable("uid") String uid) {
//        LuckUser luckUser = luckUserService.getOne(new LambdaQueryWrapper<LuckUser>().eq(LuckUser::getUid, uid));
//        return ResponseEntity.ok(luckUser);
//    }

    /**
     * 获取中奖信息
     */
    @PostMapping("/getLuckInformation")
    public  LuckUser getLuckInformation(){
        Long shopId = SecurityUtils.getSysUser().getShopId();
        LuckUser luckUser =  luckUserService.getLuckData(shopId);
        if(luckUser == null){
            LuckUser luckUser1 = new LuckUser();
            luckUser1.setShopId(shopId);
            luckUserService.saveOrUpdate(luckUser1);
        }
        return luckUser;
    }

    /**
     * 保存
     */
    @PostMapping
    public ResponseEntity<Void> save(@RequestBody @Valid LuckUser luckUser) {
        luckUser.setLuckNumber(1);
        luckUserService.save(luckUser);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改中奖信息
     */
    @PostMapping("/update")
    public ResponseEntity<String> update(LuckUser luckUser){
        String result = "success";
        Long shopId = SecurityUtils.getSysUser().getShopId();
        luckUser.setShopId(shopId);
        luckUserService.saveOrUpdate(luckUser);
        return ResponseEntity.ok(result);
    }

    /**
     * 新增判断是否已有数据,已经有了中奖数据
     */
    @GetMapping("/getData")
    public ResponseEntity<Boolean> getData(){
        boolean flag = true;
        Long shopId = SecurityUtils.getSysUser().getShopId();
        LuckUser luckUser = new LuckUser();
        luckUser.setShopId(shopId);
        LuckUser luckUser1 =  luckUserService.getLuckData(shopId);
        if(luckUser1 ==null){
            flag = false;
            return ResponseEntity.ok(flag);
        }
        return ResponseEntity.ok(flag);
    }

}
